package me.xiaosiqi.syt.hosp.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.xiaosiqi.syt.comm.utils.MD5;
import me.xiaosiqi.syt.common.exception.HospitalException;
import me.xiaosiqi.syt.common.result.ResultCodeEnum;
import me.xiaosiqi.syt.hosp.mapper.HospitalSetMapper;
import me.xiaosiqi.syt.hosp.service.HospitalSetService;
import me.xiaosiqi.syt.model.hosp.HospitalSet;
import me.xiaosiqi.syt.vo.hosp.HospitalSetQueryVo;
import org.springframework.stereotype.Service;

/**
 * @author xiaosiqi
 * @date 2022/1/25 23:00
 */
@Service
public class HospitalSetServiceImpl extends ServiceImpl<HospitalSetMapper, HospitalSet> implements HospitalSetService {
    @Override
    public Page<HospitalSet> findPage(long current, long limit, HospitalSetQueryVo hospitalSetQueryVo) {
        Page<HospitalSet> page = new Page<>(current, limit);
        LambdaQueryWrapper<HospitalSet> wrapper = new LambdaQueryWrapper<>();
        if (null != hospitalSetQueryVo) {
            String hoscode = hospitalSetQueryVo.getHoscode();
            String hosname = hospitalSetQueryVo.getHosname();
            if (StringUtils.isNotBlank(hoscode)) {
                wrapper.eq(HospitalSet::getHoscode, hoscode);
            }
            if (StringUtils.isNotBlank(hosname)) {
                wrapper.like(HospitalSet::getHosname, hosname);
            }
        }
        return this.page(page, wrapper);
    }

    @Override
    public void chikeSign(String hoscode, String sign) {
        LambdaQueryWrapper<HospitalSet> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(HospitalSet::getHoscode, hoscode);
        HospitalSet one = this.getOne(wrapper, false);
        if (null == one){
            throw new HospitalException(ResultCodeEnum.HOSCODE_NO_EXIST);
        }
        if (!StringUtils.equals(sign,MD5.encrypt(one.getSignKey()))){
            throw new HospitalException(ResultCodeEnum.SIGN_ERROR);
        }
    }
}
